package zkk.com.mengqu.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import zkk.com.mengqu.R;
import zkk.com.mengqu.adapter.SubmitOrderAdapter;
import zkk.com.mengqu.manager.APIManager;
import zkk.com.mengqu.models.Address;
import zkk.com.mengqu.models.Food;
import zkk.com.mengqu.simplepreferences.Share;
import zkk.com.mengqu.util.FinalString;
import zkk.com.mengqu.util.NoScrollListView;

/**
 * Created by Administrator on 2017/5/8.
 * 提交订单
 */
public class SubmitOrderActivity extends Activity implements View.OnClickListener{
    private Context context;
    private ImageView back;//返回
    private ListView listView;
    private SubmitOrderAdapter adapter;
    private TextView txPrice,txCoupon,txAllPrice;//总价格、优惠数、优惠后总价格
    private TextView txSubmitCoupon,txSubmitAllPrice;//提交优惠数、提交价格
    private TextView submitOrder;//提交订单
//  订单地址
    private TextView userName, userPhone, userSex, userAddress;

    private RelativeLayout rlAddress;

    private int coupon = 0;//优惠价格
    private int couponPrice = 0;//优惠后价格

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submit_order);
        initView();
        getAddress();
    }

    public void initView(){
        context = getApplicationContext();
        back = (ImageView) findViewById(R.id.iv_back);
        //列表
        listView = (NoScrollListView)findViewById(R.id.listview);
        adapter = new SubmitOrderAdapter(context,toList());
        listView.setAdapter(adapter);

        //订单
        txPrice = (TextView)findViewById(R.id.order_total);
        txCoupon = (TextView)findViewById(R.id.order_coupon);
        txAllPrice = (TextView)findViewById(R.id.tx_all_price);

        //确认订单
        txSubmitCoupon = (TextView)findViewById(R.id.tx_submit_order_coupon);
        txSubmitAllPrice = (TextView)findViewById(R.id.tx_submit_all_price);

        //订单地址
        rlAddress = (RelativeLayout) findViewById(R.id.rl_choose_address);
        userName = (TextView)findViewById(R.id.tx_user_name);
        userSex = (TextView)findViewById(R.id.tx_user_gender);
        userPhone = (TextView)findViewById(R.id.tx_user_phone);
        userAddress = (TextView)findViewById(R.id.tx_user_address);

        //提交订单
        submitOrder = (TextView)findViewById(R.id.tx_submit_order);


        int allPrice = Integer.parseInt(getIntent().getStringExtra("allPrice"));
        couponPrice = allPrice - coupon;

        txPrice.setText("" + allPrice);
        txCoupon.setText("" + coupon);
        txAllPrice.setText("" + couponPrice);

        txSubmitCoupon.setText("" + coupon);
        txSubmitAllPrice.setText("" + couponPrice);

        back.setOnClickListener(this);
        submitOrder.setOnClickListener(this);

        rlAddress.setOnClickListener(this);

        Log.w("allPrice",""+allPrice);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tx_submit_order:
                Toast.makeText(context,"Submit Order",Toast.LENGTH_LONG).show();
                break;
            case R.id.rl_choose_address:
                Intent intent = new Intent(SubmitOrderActivity.this, AddressActivity.class);
                intent.putExtra(FinalString.ADDRESS_TYPE, FinalString.CHOOSE_ADDRESS);
                startActivityForResult(intent, FinalString.CHOOSE_ADDRESS);
                break;
            default:
                Toast.makeText(context,"Not Find",Toast.LENGTH_LONG).show();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case FinalString.CHOOSE_ADDRESS:
                String name = data.getStringExtra(FinalString.ADDRESS_NAME);
                int sex = data.getIntExtra(FinalString.ADDRESS_SEX, -1);
                String phone = data.getStringExtra(FinalString.ADDRESS_PHONE);
                String address = data.getStringExtra(FinalString.ADDRESS);
                userName.setText(name);
                userSex.setText(sex == 0 ? "先生" : "女士");
                userPhone.setText(phone);
                userAddress.setText(address);
                break;
        }
    }

    public void getAddress(){
        int uid = Integer.parseInt(Share.getUserId());
        APIManager.setManager()
                .getAddresses(uid)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Address>>() {
                    @Override
                    public void onCompleted() {
                        Log.w("onCompleted-Submit" , "onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.w("onError-getAddress" , "onError");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<Address> addresses) {
                        for (Address address : addresses){
                            if (address.isIdDefault()){
                                userName.setText(address.getName());
                                userSex.setText(address.getSex() == 0 ? "先生" : "女士");
                                userPhone.setText(address.getPhone());
                                userAddress.setText(address.getAddress());
                            }
                        }
                    }
                });
    }
    //将Map转化成List
    public List<Food> toList(){
        Map<Integer,Food> foodMap = FinalString.foodMap;
        List<Food> foodList = new ArrayList<>();
        for (int id : foodMap.keySet()){
            foodList.add(foodMap.get(id));
        }
        Log.w("foodList" , ""+ foodList.size());
        return foodList;
    }
}
